package com.piaodekuai.flight.web;

import com.piaodekuai.flight.pojo.User;
import com.piaodekuai.flight.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//@RestController
@Controller
public class bookingController {

    @Autowired
    UserService userService;

    @RequestMapping("/Home")
    public String home(Model m, HttpServletRequest request) throws Exception {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("userName1="+username + ", passWord1="+ password);

        boolean haveUser = false;
        //serService userService = new UserService();
        System.out.println("ok");
        List<User> users = userService.userTest();

        for (int i = 0; i<users.size();i++){

            String username2 = users.get(i).getuName();
            String password2 = users.get(i).getuPassword();

            if (username2.equals(username) && password2.equals(password)){
                haveUser = true;
                System.out.println("haveUser2="+haveUser);
            }

        }

        m.addAttribute("haveUser", haveUser);


        if(false){
            throw new Exception("some exception");
        }

        //return "Hello Spring Boot!";
        return "Home";
    }

    @RequestMapping("/quit")
    public String quit(Model m) throws Exception {

        boolean haveUser = false;
        m.addAttribute("haveUser", haveUser);
        if(false){
            throw new Exception("some exception");
        }
        System.out.println("quit");
        //return "Hello Spring Boot!";
        return "Home";
    }

    @RequestMapping("/oneway")
    public String oneway(Model m) throws Exception {


        if(false){
            throw new Exception("some exception");
        }

        //return "Hello Spring Boot!";
        return "oneway";
    }

    @RequestMapping("/passenger")
    public String passenger(Model m) throws Exception {

        if(false){
            throw new Exception("some exception");
        }

        //return "Hello Spring Boot!";
        return "passenger";
    }

    @RequestMapping("/pay")
    public String onePay(Model m) throws Exception {

        if(false){
            throw new Exception("some exception");
        }

        //return "Hello Spring Boot!";
        System.out.println("来了");
        return "pay";
    }

    @RequestMapping("/continuePay")
    public String twoPay(Model m) throws Exception {

        if(false){
            throw new Exception("some exception");
        }

        //return "Hello Spring Boot!";
        return "paysuccess";
    }

}
